//如果登录 改变布局
function changeLogin(){
    let login=JSON.parse(localStorage.getItem('login')||"[]")
    let iflogin=document.querySelector('.iflogin')
    if(login!=''){
     
       iflogin.innerHTML=`用户<a href='#'>${login.username}</a>`
       iflogin.nextSibling.innerHTML=`<a href='./cart.html'>我的订单</a>`
    }else{
        iflogin.innerHTML=`<a href="./login.html">登录</a>`
        iflogin.nextSibling.innerHTML=`<a href="./register.html">请注册</a>`
    }   
}
changeLogin()


//退出登录
let exit=document.querySelector('.exit')
exit.onclick=function(){
    if(confirm('退出登录?')){
        localStorage.removeItem('login')
        location.reload();
    }
}


let eliminate=document.querySelector('.eliminate');
eliminate.onclick=function(){
    
    this.parentNode.style.display='none'
}


function showList(){
let list=JSON.parse(localStorage.getItem('navList')||'[]')

let str=list.reduce((pre,cur)=>{
    return pre+=`
    <li data-id=${cur.id}>
    <div class="remodlist_img">
        <img src="${cur.picture}" alt="">
    </div>
    <div class="recomlist_text">
      <div class="oneline">  
          <span>价保618</span>
        <span class="spanp">${cur.shop}</span></div>
    
        <span class="recomprice">$${cur.price}</span>
    </div>
 
</li>
    `
},'')
let productOul=document.querySelector('.remodlist').querySelector('ul')
productOul.innerHTML=str
clickGood()
}


//再次点击其他分类

let navList=document.querySelector('.navP').querySelectorAll('a')
for(let i=0;i<navList.length;i++){
    navList[i].setAttribute('data-id',i)
    navList[i].addEventListener('click',function(){
       let index=this.getAttribute('data-id')
       getNavList(index)
    })
}


function getNavList(id){
    axios({
        method:'get',
        url:'http://api.yuguoxy.com/api/shop/list/category',
        params:{
            categoryId:id
        },
    }).then(response=>{
        let data=response.data
        let {resultCode,resultInfo} =data;
        if(resultCode==1){
            let navList=resultInfo.list
            localStorage.setItem('navList',JSON.stringify(navList))
            showList()
        }
    })
}

showList()



//点击商品跳转详情
function clickGood(){
    let productOul=document.querySelector('.remodlist').querySelector('ul')
    
    let lis=productOul.children;
    console.log(lis);
    [...lis].forEach(item=>{
        item.addEventListener('click',function(){
            let goodId=this.getAttribute('data-id')
            location.href=`../pages/detail.html?id=${goodId}`
        })
    })
}

//搜索
let svg=document.querySelector('.svg')
svg.onclick=function(){
    let search=document.querySelector('.search')
    let val=search.value;
    if(val==''){
        alert('请输入搜索内容')
    }else{
        location.href=`../pages/find.html?keyword=${val}`
       
    }
   
    
}


//分类列表

